//
//  ViewController.m
//  NSProgress
//
//  Created by 韩艳锋 on 2021/11/15.
//

#import "ViewController.h"
#import "NSProgressMg.h"

@import Masonry;

@interface ViewController ()
@property (nonatomic, strong) UILabel * label1;
@property (nonatomic, strong) UILabel * label2;
@property (nonatomic, strong) UILabel * label3;
@property (nonatomic, strong) NSProgressMg * mg;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.label1 = [[UILabel alloc] init];
    [self.view addSubview:self.label1];
    [self.label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(50);
    }];
    
    self.label2 = [[UILabel alloc] init];
    [self.view addSubview:self.label2];
    [self.label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.label1.mas_bottom);
    }];
    
    self.label3 = [[UILabel alloc] init];
    [self.view addSubview:self.label3];
    [self.label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.label2.mas_bottom);
    }];
    
    self.mg = [NSProgressMg explicitly:YES];
    
    
    [self.mg.progress1 addObserver:self forKeyPath:@"completedUnitCount" options:NSKeyValueObservingOptionNew context:nil];
    [self.mg.progress2 addObserver:self forKeyPath:@"completedUnitCount" options:NSKeyValueObservingOptionNew context:nil];
    [self.mg.progress3 addObserver:self forKeyPath:@"completedUnitCount" options:NSKeyValueObservingOptionNew context:nil];
    
    UISlider * slider2 = [[UISlider alloc] init];
    UISlider * slider3 = [[UISlider alloc] init];
    slider2.minimumValue = 0;
    slider2.maximumValue = self.mg.progress2.totalUnitCount;
    
    slider3.minimumValue = 0;
    slider3.maximumValue = self.mg.progress3.totalUnitCount;

    [self.view addSubview:slider2];
    [slider2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
        make.top.equalTo(self.label3.mas_bottom);
    }];
    
    [self.view addSubview:slider3];
    [slider3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
        make.top.equalTo(slider2.mas_bottom);
    }];
    
    [slider2 addTarget:self action:@selector(slider2:) forControlEvents:UIControlEventValueChanged];
    [slider3 addTarget:self action:@selector(slider3:) forControlEvents:UIControlEventValueChanged];
    [self refresh1];
    [self refresh2];
    [self refresh3];
    
    UIButton * btnReset = [[UIButton alloc] init];
    [btnReset setTitle:@"  reset  " forState:UIControlStateNormal];
    btnReset.backgroundColor = [UIColor redColor];
    [btnReset addTarget:self.mg action:@selector(reset) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnReset];
    
    [btnReset mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
        make.top.equalTo(slider3.mas_bottom);
    }];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if (self.mg.progress1 == object) {
        [self refresh1];
    } else if (self.mg.progress2 == object) {
        [self refresh2];
    } else if (self.mg.progress3 == object) {
        [self refresh3];
    }
}

- (void)refresh1 {
    self.label1.text = [NSString stringWithFormat:@"Progress1 total：%lld complete：%lld", self.mg.progress1.totalUnitCount, self.mg.progress1.completedUnitCount];
}

- (void)refresh2 {
    self.label2.text = [NSString stringWithFormat:@"Progress2 total：%lld complete：%lld", self.mg.progress2.totalUnitCount, self.mg.progress2.completedUnitCount];
}

- (void)refresh3 {
    self.label3.text = [NSString stringWithFormat:@"Progress3 total：%lld complete：%lld", self.mg.progress3.totalUnitCount, self.mg.progress3.completedUnitCount];
}


- (void)slider2:(UISlider*)sender {
    self.mg.progress2.completedUnitCount = sender.value;
}

- (void)slider3:(UISlider*)sender {
    self.mg.progress3.completedUnitCount = sender.value;
}
@end

